
import { request,BASEURL} from '../../utils/request';
Page({
  data: {
    name: '',
    phone: '',
    region: ['河南省','郑州市','金水区'],
    uaddress: '',
    openid:'',
  },
  //form表单
  async submitForm(e) {
    // console.log(e)
let openid = this.data.openid
    let region = this.data.region
    let {
      name,
      phone,
      uaddress
    } = e.detail.value
    if(name==''||phone==''||region==undefined||uaddress==''){
      wx.showToast({
        title: '必要信息不能为空',
        icon:'none'
      })
      return
    }
  //修改地址的数据  goodsAddress

    let res = await request({
      url:'/api/Wxapp/setAddress',
      method:'POST',
      data:{
        openid,
        name,
        phone,
        addrinfo:uaddress,
        province:region[0],
        city:region[1],
        county:region[2],
      },
    })
    // console.log(res)
    if(res.data.code==0){
      // res.data.msg 提示信息
      this.showFrame('地址未修改')
      return
    }else{
      wx.showToast({
        title:res.data.msg,
        duration:1500,
        mask:true,
        success:()=>{
          setTimeout(()=>{
            wx.navigateBack({
              delta: 1,
            })
          },1500)
        }
      })
    }
  },
  //
  showFrame: function (text) {
    // console.log(text)
    wx.showToast({
      title: text,
      icon: "none",
      duration: 1500
    })
  },

  //region的值
  bindRegionChange(e) {
    console.log(e.detail.value)
    let value = this.data.region
    value = e.detail.value
    this.setData({
      region: value
    })
  },
  onShow: function () {
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取openid
    let {openid} = wx.getStorageSync('uid')
    //拿到本地存储中的地址
    let address = wx.getStorageSync('address')
    let {region} = this.data
    if(address==null){
      this.setData({
        openid
      })
    }else{
      region[0] = address.province
      region[1] = address.city
      region[2] = address.county
      this.setData({
        name: address.name,
        phone: address.phone,
        uaddress: address.addrinfo,
        openid,
        region
      })
      this.setData({
        openid
      })
    }
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let {openid} = wx.getStorageSync('uid')
    return {
      title: "邀请好友",
      path:`/pages/index/index?pid=${openid}` 
    }
  }
})